package com.neulogistics.infrastructure.integrationeventlog.model;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;

import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.blackto.lib.eventbus.event.IntegrationEvent;
import lombok.Data;

/**
 * 集成事件日志表
 * @TableName tb_integration_event_log
 */
@TableName(value ="tb_integration_event_log", autoResultMap = true)
@Data
public class IntegrationEventLogEntry implements Serializable {
    /**
     * Log id
     */
    @TableId(value = "event_id", type = IdType.INPUT)
    private Long eventId;

    /**
     * 事件全类名
     */
    @TableField(value = "event_type")
    private String eventType;

    /**
     * 事件名
     */
    @TableField(exist = false)
    private String eventName;

    @TableField(exist = false)
    private IntegrationEvent integrationEvent;

    /**
     * 事件状态：0:未发布；1:发布中；2:已发布；3:发布失败
     */
    @TableField(value = "event_state")
    private Integer eventState;

    /**
     * 发送次数
     */
    @TableField(value = "times_sent")
    private Integer timesSent;

    /**
     * 创建时间
     */
    @TableField(value = "create_time")
    private Date createTime;

    /**
     * 事件内容
     */
    @TableField(value = "content_json", typeHandler = JacksonTypeHandler.class)
    private JSONObject contentJson;

    /**
     * 事务id
     */
    @TableField(value = "transaction_id")
    private Long transactionId;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    public String getEventName(){
        // 这个 '.'是大坑啊！！！必须转义
        String[] split = eventType.split("\\.");
        return split[split.length-1];
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", eventId=").append(eventId);
        sb.append(", eventType=").append(eventType);
        sb.append(", eventState=").append(eventState);
        sb.append(", timesSent=").append(timesSent);
        sb.append(", createTime=").append(createTime);
        sb.append(", contentJson=").append(contentJson);
        sb.append(", transactionId=").append(transactionId);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }

    public IntegrationEventLogEntry deserializeIntegrationEvent(
            Class<? extends IntegrationEvent> type){
        integrationEvent = JSONUtil.toBean(contentJson, type);
        return this;
    }

    public IntegrationEventLogEntry(){}

    public IntegrationEventLogEntry(IntegrationEvent integrationEvent, long transactionId){
        eventId = integrationEvent.getEventId();
        createTime =integrationEvent.getCreatedTime();
        eventType = integrationEvent.getClass().getName();
        contentJson = JSONUtil.parseObj(integrationEvent);
        eventState = EventState.NotPublished.getState();
        timesSent = 0;
        this.transactionId = transactionId;
    }
}